package userlogservice

import (
	"encoding/json"
	"strings"
	"sync"
	"vpn-management-server/config"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/src/model/analysismodel"
	"vpn-management-server/src/model/userlogmodel"
	"vpn-management-server/src/service/userservice"
)

// log缓冲池
type logPool struct {
	logQ     chan interface{}
	connPool sync.Pool
}

// 构造函数
func newLogPool(f func() interface{}, size int) logPool {
	return logPool{
		logQ: make(chan interface{}, size),
		connPool: sync.Pool{
			New: f,
		},
	}
}

func Init(config *config.VPNConfig) {
	nLogPool = newLogPool(func() interface{} {
		return network.GetBCSSLConn()
	}, config.NPoolSize)
	go SendUserLogToBC()
}

var nLogPool logPool

func GetPageNormalLogsByTimestamp(getLogs *userlogmodel.GetPageLogs) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetPageNormalLogsByTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetPageWarningLogsByTimeStamp(getLogs *userlogmodel.GetPageLogs) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetPageWarningLogsByTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetAllNormalLogsByTimestamp(getLogs *userlogmodel.GetAllLogs) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetAllNormalLogsByTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetAllWarningLogsByTimestamp(getLogs *userlogmodel.GetAllLogs) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetAllWarningLogsByTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetPageNormalLogsByUGroupIDAndTimestamp(getLogs *userlogmodel.GetPageLogsByUGroupID) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetPageNormalLogsByUGroupIDAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetPageWarningLogsByUGroupIDAndTimestamp(getLogs *userlogmodel.GetPageLogsByUGroupID) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetPageWarningLogsByUGroupIDAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetAllNormalLogsByUGroupIDAndTimestamp(getLogs *userlogmodel.GetAllLogsByUGroupID) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetAllNormalLogsByUGroupIDAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetAllWarningLogsByUGroupIDAndTimestamp(getLogs *userlogmodel.GetAllLogsByUGroupID) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetAllWarningLogsByUGroupIDAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail,", err)
		return nil, -1
	}

	return result.Logs, result.Count
}

func GetPageNormalLogsByUserNameAndTimestamp(getLogsByUserName *userlogmodel.GetLogsByUserName) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogsByUserName)
	request.Type = "userlog"
	request.Command = "GetPageNormalLogsByUserNameAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	json.Unmarshal(res.Data, &result)

	return result.Logs, result.Count
}

func GetPageWarningLogsByUserNameAndTimestamp(getLogsByUserName *userlogmodel.GetLogsByUserName) ([]*userlogmodel.UserLog, int) {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil, -1
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogsByUserName)
	request.Type = "userlog"
	request.Command = "GetPageWarningLogsByUserNameAndTimestamp"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil, -1
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil, -1
	}
	var result = struct {
		Logs  []*userlogmodel.UserLog
		Count int
	}{}
	json.Unmarshal(res.Data, &result)

	return result.Logs, result.Count
}

func UploadUserLogByMIN(log []byte) {
	normalLog := new(userlogmodel.UserLog)
	if err := json.Unmarshal(log, normalLog); err == nil {

		user := userservice.GetUserByName(normalLog.Name)
		if user == nil {
			logger.Logger.Error("user not exit")
			return
		}
		normalLog.UGroupID = user.UGroupID

		UpLoadUserLog(normalLog)
	} else {
		logger.Logger.Error(err.Error())
	}
}

func UpLoadUserLog(userLog *userlogmodel.UserLog) {
	userLog.FilterWebSite = websiteFilter(userLog.WebSite)
	logger.Logger.Debug("website:", userLog.WebSite, " filterwebsite:", userLog.FilterWebSite, " isInner:", userLog.IsInner, " level:", userLog.Level)
	nLogPool.logQ <- userLog
}

func SendUserLogToBC() {
	// 使用同一个变量
	var request = network.NewNetRequest()
	request.Type = "userlog"
	request.Command = "UploadUserLog"
	request.Method = "POST"
	request.Header["Mode"] = "long"
	for log := range nLogPool.logQ {
		// chan已经保证了互斥
		conn, ok := nLogPool.connPool.Get().(network.Connect)
		// 从池中获取一个连接 如果不够会新建一个
		if !ok {
			logger.Logger.Error("get conn from bc fail")
			continue
		}
		userlog := log.(*userlogmodel.UserLog)

		data, _ := json.Marshal(log)
		request.Parameters = data
		err := network.SendRequest(request, conn)
		if err != nil {
			logger.Logger.Error("fail to send request to blockchain")
			_ = conn.Close()
			continue
		}
		res, err := network.GetResponse(conn)
		if err != nil {
			logger.Logger.Error("fail to get response from blockchain,error:", err.Error())
			_ = conn.Close()
			continue
		}
		if res.Code != 200 {
			logger.Logger.Error("upload log fail ,error:", res.ErrorMsg)
		} else {
			logger.Logger.Info(getPrintInfo(userlog.Level, userlog.IsInner))
		}
		// 连接放回连接池，复用连接
		nLogPool.connPool.Put(conn)
	}
}

func GetWarnLogsAnalysis(getLogs *analysismodel.GetDataAnalysis) []int {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetWarningLogsAnalysis"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}
	var result = struct {
		Analysis []int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}
	if result.Analysis == nil {
		logger.Logger.Warn("warning log analysis is empty")
		result.Analysis = []int{}
	}

	return result.Analysis
}

func GetNormLogsAnalysis(getLogs *analysismodel.GetDataAnalysis) []int {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetNormalLogsAnalysis"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}
	var result = struct {
		Analysis []int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}
	if result.Analysis == nil {
		logger.Logger.Warn("normal log analysis is empty")
		result.Analysis = []int{}
	}

	return result.Analysis
}

// GetUGroupLogNumByUGroupID 获取用户组告警日志数量
func GetUGroupLogNumByUGroupID(ugid int) *userlogmodel.LogNum {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	var request network.NetRequest
	var ugroupID = userlogmodel.UGroupID{
		UGroupID: ugid,
	}
	data, _ := json.Marshal(ugroupID)
	request.Type = "userlog"
	request.Command = "getUGroupLogNumByUGroupID"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}
	var result userlogmodel.LogNum
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}
	//conn.Close()
	return &result
}

func GetWarnLogsAnalysisByUGroupID(getLogs *analysismodel.GetUGroupDataAnalysis) []int {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetWarningLogsAnalysisByUGroupID"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}
	var result = struct {
		Analysis []int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}
	if result.Analysis == nil {
		logger.Logger.Warn("ugroup warning log analysis is empty")
		result.Analysis = []int{}
	}

	return result.Analysis
}

func GetNormLogsAnalysisByUGroupID(getLogs *analysismodel.GetUGroupDataAnalysis) []int {
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	var request network.NetRequest
	data, _ := json.Marshal(getLogs)
	request.Type = "userlog"
	request.Command = "GetNormalLogsAnalysisByUGroupID"
	request.Method = "GET"
	request.Parameters = data
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}
	var result = struct {
		Analysis []int
	}{}
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}
	if result.Analysis == nil {
		logger.Logger.Warn("ugroup normal log analysis is empty")
		result.Analysis = []int{}
	}

	return result.Analysis
}

func GetLogsAnalysisByUser(logType *analysismodel.LogType) map[string]int {
	// 获取和区块链之间的连接
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}

	defer conn.Close()

	data, _ := json.Marshal(logType)

	// new一个request
	var request network.NetRequest
	request.SetType("userlog").SetCommand("getLogsAnalysisByUser").SetMethod("GET").SetParameters(data)

	// 发送request
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}

	// 得到response
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}

	// 得到日志提取分析
	var result analysismodel.LogsAnalysis
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}

	// 如果为空 说明没有分析 数据 返回初始化的map
	if result.Analysis == nil {
		logger.Logger.Warn("warning log analysis is empty")
		result.Analysis = make(map[string]int)
	}

	return result.Analysis
}

func GetLogsAnalysisByUserAndUGroupID(ugroupLogType *analysismodel.UGroupLogType) map[string]int {
	// 获取和区块链之间的连接
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	data, _ := json.Marshal(ugroupLogType)

	// new一个request
	var request network.NetRequest
	request.SetType("userlog").SetCommand("getLogsAnalysisByUserAndUGroupID").SetParameters(data).SetMethod("GET")

	// 发送request
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}

	// 得到response
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}

	// 得到日志提取分析
	var result analysismodel.LogsAnalysis
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}

	// 如果为空 说明没有分析 数据 返回初始化的map
	if result.Analysis == nil {
		logger.Logger.Warn("warning log analysis is empty")
		result.Analysis = make(map[string]int)
	}

	return result.Analysis
}

func GetLogsAnalysisByWebsite(logType *analysismodel.LogType) map[string]int {
	// 获取和区块链之间的连接
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	data, _ := json.Marshal(logType)

	// new一个request
	var request network.NetRequest
	request.SetType("userlog").SetCommand("getLogsAnalysisByWebsite").SetMethod("GET").SetParameters(data)

	// 发送request
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}

	// 得到response
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}

	// 得到日志提取分析
	var result analysismodel.LogsAnalysis
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}

	// 如果为空 说明没有分析 数据 返回初始化的map
	if result.Analysis == nil {
		logger.Logger.Warn("warning log analysis is empty")
		result.Analysis = make(map[string]int)
	}

	//_ = conn.Close()

	return result.Analysis
}

func GetLogsAnalysisByWebsiteAndUGroupID(ugroupLogType *analysismodel.UGroupLogType) map[string]int {
	// 获取和区块链之间的连接
	conn := network.GetBCSSLConn()
	if conn == nil {
		logger.Logger.Error("connect to blockchain fail")
		return nil
	}
	defer conn.Close()

	data, _ := json.Marshal(ugroupLogType)

	// new一个request
	var request network.NetRequest
	request.SetType("userlog").SetCommand("getLogsAnalysisByWebsiteAndUGroupID").SetParameters(data).SetMethod("GET")

	// 发送request
	err := network.SendRequest(&request, conn)
	if err != nil {
		logger.Logger.Error("fail to send request to blockchain")
		return nil
	}

	// 得到response
	res, err := network.GetResponse(conn)
	if err != nil {
		logger.Logger.Error("fail to get response from blockchain")
		return nil
	}

	// 得到日志提取分析
	var result analysismodel.LogsAnalysis
	err = json.Unmarshal(res.Data, &result)
	if err != nil {
		logger.Logger.Error("UnMarshal Parameters fail, ", err)
		return nil
	}

	// 如果为空 说明没有分析 数据 返回初始化的map
	if result.Analysis == nil {
		logger.Logger.Warn("warning log analysis is empty")
		result.Analysis = make(map[string]int)
	}

	//_ = conn.Close()

	return result.Analysis
}

func websiteFilter(website string) string {
	webArray := strings.Split(website, "/")

	if strings.Contains(webArray[0], "http") {
		if len(webArray) < 3 {
			return strings.Replace(website, "/", "", -1)
		}
		return strings.Join(webArray[2:3], "/")
	}
	return strings.Join(webArray[:1], "/")
}

func getPrintInfo(level int, isInner int) string {
	var info string

	switch level {
	case 1:
		info += "upload normal log success,"
	case 2:
		info += "upload warning log success,"
	default:
		info += "upload unsupported log success,"
	}

	switch isInner {
	case 0:
		info += "log type is innerToInner"
	case 1:
		info += "log type is innerToOuter"
	case 2:
		info += "log type is outerToInner"
	default:
		info += "log type is unsupported"
	}

	return info
}
